package cn.edu.ujn.lab3.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.lab3.dao.BaseDict;
import cn.edu.ujn.lab3.dao.BaseDictMapper;
import cn.edu.ujn.lab3.dao.Customer;
import cn.edu.ujn.lab3.dao.CustomerMapper;
import cn.edu.ujn.lab3.dao.CustomerQueryVo;
import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.dao.UserMapper;
import cn.edu.ujn.lab3.service.ICustomerService;
import cn.edu.ujn.lab3.util.CheckName;
import cn.edu.ujn.lab3.util.CheckPhone;
import cn.edu.ujn.lab3.util.CheckPostCode;
import cn.edu.ujn.lab3.util.CheckTelePhone;
import cn.edu.ujn.lab3.util.Page;

@Service("customerService")
@Transactional
public class CustomerServiceImpl implements ICustomerService {

	// 注入BaseDictMapper类
	@Autowired
	private BaseDictMapper dictMapper;
	// 注入CustomerMapper类
	@Autowired
	private CustomerMapper customerMapper;

	// 根据行业编号查找指定信息
	@Override
	@Transactional
	public List<BaseDict> findDictByCode(String code) {
		List<BaseDict> list = dictMapper.findDictByCode(code);
		return list;
	}
  
	// 根据查询条件查询用户
	@Override
	@Transactional
	public List<Customer> findCustomerByVo(CustomerQueryVo vo) {
		List<Customer> list = customerMapper.findCustomerByVo(vo);
		return list;
	}

	// 根据查询条件查询相关用户的总数量
	@Override
	@Transactional
	public Integer findCustomerByVoCount(CustomerQueryVo vo) {
		Integer count = customerMapper.findCustomerByVoCount(vo);
		return count;
	}

	@Override
	@Transactional
	public int insert(Customer record) {
		int insert = customerMapper.insert(record);
		return insert;
	}

	// 用户删除实现
	@Override
	@Transactional
	public Integer deleteByPrimaryKey(Integer custId) {
		// TODO Auto-generated method stub
		Integer number = customerMapper.deleteByPrimaryKey(custId);
		return number;
	}

	@Override
	@Transactional
	public Customer getCustomerById(Long custId) {
		// 根据ID查找用户
		return customerMapper.selectByPrimaryKey(custId);
	}

	@Override
	@Transactional
	public int upCustomerById(Customer record) {
		// TODO Auto-generated method stub
		// 根据ID修改用户
		return customerMapper.updateByPrimaryKeySelective(record);
	}
	@Override
	@Transactional
	public boolean checkCustomer(Customer record) {
		// TODO Auto-generated method stub
		//检验信息的正确性
		if(!CheckName.validateName(record.getCustName()) ||
		   !CheckPhone.isPhoneLegal(record.getCustMobile()) ||
		   !CheckPostCode.checkPostCode(record.getCustZipcode()) ||
		   !CheckTelePhone.telePhoneNumber(record.getCustPhone()) ||
		   "".equals(record.getCustIndustry()) ||
		   "".equals(record.getCustLevel()) ||
		   "".equals(record.getCustSource())
		   )
		return false;
		return true;
	}
}
